// 滑动窗口 思维
function lengthOfLongestSubstring (s) {
  if(s == null || s == '')return 0;
  let arr = [];


  let left = 0;
  let right = 0;
  let sarr = s.split('')
  let vw = [sarr[left]]
  // console.log(sarr)
  
   
  while( left < sarr.length && right<=sarr.length - 1){
    // console.log('right',right)
    // console.log('left',left)
    if(right >= sarr.length){
      if(sarr[left] == sarr[right]){
         arr.push(1)
      }else{
           arr.push(right - left + 1)
      }
    }
    // console.log('vw',vw,vw.indexOf(sarr[right]))
     if(vw.indexOf(sarr[right]) != -1){
      if(left == right){
        arr.push(1)
        right++
      }else{
        if(sarr[left] == sarr[right]){
         
          left ++
          // right=left
          // vw = []
          vw.shift()
          vw.push( sarr[right])
          right++
         
          // arr.push(right - left)
        }else{
         
          // console.log('vw',vw)
          // console.log('left',left)
   
          // console.log('sarr[right]',sarr[right])
          // console.log(vw.indexOf(sarr[right]))
          // console.log('重复位置',left + vw.indexOf(sarr[right]) + 1)
          left = left + vw.indexOf(sarr[right]) + 1
          right=left
         
          vw = []
         
          vw.push(sarr[left])
        }
        
      }
     }else{
      // console.log('2')
      arr.push(right - left + 1)
      vw.push(sarr[right])
      right++
      
     }
  }
// console.log(arr)
getme()
  return Math.max(...arr)
};

// console.log('xxxxxxxxxx',lengthOfLongestSubstring('abcabcbb'))

// console.log('0000000000',lengthOfLongestSubstring('pwwkew'))

function func1(s) {
  let max = 0;
    let left = 0; // 左指针
    let map = new Map(); // map判断重复
    let sarr = s.split('')

    sarr.forEach((item, index) => {
        if (map.has(sarr[index]) && map.get(sarr[index]) >= left) {
            left = map.get(sarr[index]) + 1;
        }
        console.log(map)
        map.set(sarr[index], index);
        max = Math.max(max, index-left+1);
    });
    getme()
    return max;
}
var startTempVariable = new Date();
 console.log('xxxxxxxxxx',func1('abcabcbb'))
 console.log(`destructuring: ${new Date() - startTempVariable}ms`);
 console.log('xxxxxxxxxx',lengthOfLongestSubstring('abcabcbb'))
 console.log(`destructuring: ${new Date() - startTempVariable}ms`);

 // 模仿内存回收 
var size = 20 * 1024 * 1024
var arrall = []

function getme() {
    var mem = process.memoryUsage()
    console.log(mem)
    var format = function(bytes) {
        return (bytes / 1024 / 1024).toFixed(2) + 'MB';
    }
    console.log('heapTotal: ' + format(mem.heapTotal) + 'heapUsed: ' + format(mem.heapUsed))
}

// getme()

// // v8引擎只有1.4g内存可以支配
// for (var i = 0; i < 20; i++) {
//     if (arrall.length > 4) {
//         arrall.shift()
//     }
//     arrall.push(new Array(size))
//     getme()
// }